#include "coin2/flow/reader.h"
#include <cxxopts.hpp>
// #include "coin/proto/coin_market_query.pb.h"
#include "coin2/exchange/base/executor/topic_publisher.h"
#include "coin/proto/coin_feed.pb.h"

using namespace coin2::flow;

class TickerDumper{
 public:
  TickerDumper(int64_t start_ts, int64_t end_ts, std::set<std::string> topics) {
    reader_ = std::make_unique<FlowReader>(
      "main",
      "/home/ziyan/data/sub_proto_log",
      topics,
      start_ts,
      end_ts,
      10485760,
      false);
  }

  void dump(){
    int count = 0;
    while (reader_->Read(&packet_)) {
      rebuild_proto(packet_.payload);
      LOG(INFO) << "---------" << count << "-----------";
      count++;
    }
  }

  void rebuild_proto(std::string payload){
    coin::proto::SubscriptionLog proto;
    proto.ParseFromString(payload);
    LOG(INFO) << proto.mutable_ticker()->exchange();
    LOG(INFO) << proto.mutable_ticker()->query_ts();
    // for(auto& product_ticker : proto.mutable_ticker()->each_ticker()){
    //   LOG(INFO) << product_ticker.symbol() << " | "<< product_ticker.mid() << " | "<< product_ticker.exchange_timestamp() << " | "<< product_ticker.interval_timestamp();
    // }
  }

 protected:
  std::unique_ptr<FlowReader> reader_;
  coin2::exchange::base::executor::TopicRecordPacket packet_;
};


int main(int argc, char* argv[]) {
  std::set<std::string> mea;
  mea.insert("spot_gdax_v1");
  // machine.insert("spot_okex_v5");
  cxxopts::Options opt("dumper", "dump interval mid price");
  opt.add_options()("start_ts","start timestamp",cxxopts::value<int64_t>()->default_value("0"));
  opt.add_options()("end_ts","end timestamp",cxxopts::value<int64_t>()->default_value("0"));
  auto res = opt.parse(argc, argv);
  TickerDumper dumper(res["start_ts"].as<int64_t>(), res["end_ts"].as<int64_t>(), mea);
  dumper.dump();
  return 0;
}
